//
//  WetherModel.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/11.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "WetherModel.h"

@implementation WetherModel

#define HTTPURL @"http://apis.baidu.com/heweather/weather/free"
#define API_KEY @"4fc0970fc22f504982c1fab95ab9c539"

- (void)temperatureListCity:(NSString *)city {
    NSString *urlString = [NSString stringWithFormat:@"%@?city=%@", HTTPURL, city];
    NSString *urlUTF8 = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString: urlUTF8];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: API_KEY forHTTPHeaderField: @"apikey"];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error.localizedDescription);
        } else {
            id responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.temperatureList = responseObject[@"HeWeather data service 3.0"][0];
            });
        }
    }];
    [dataTask resume];
}

@end
